/**
 * 编辑后台管理页js
 * @authors dr2009 (1085388169@qq.com)
 * @date    2014-10-14 14:57:05
 * @version 1.0
 */
$(function() {
    /*-----------------------------------

                    通用

    ------------------------------------*/
    //主菜单
    $("#nav").on('click', 'li', function() {
        $(this).addClass('actived').siblings().removeClass('actived');
        var index = $('#nav li').index($(this));
        if (index === 0) {
            $("#left", window.parent.document).hide();
            $("#main", window.parent.document).css({
                width: $(window).width() + "px",
                left: 0
            });
        } else {
            $("#left", window.parent.document).show();
            // $("#left",window.parent.document).find('.submenu li.actived').trigger("click");
            $("#main", window.parent.document).css({
                width: $(window).width() - 191 + "px",
                left: "191px"
            });
        }
    });
    // 次级菜单
    $(".submenu").on('click', 'li', function() {
        $(".submenu li").removeClass('actived');
        $(this).addClass('actived');
    });
    //选项卡
    $(".tab-menu").on('click', 'li', function() {
        $(this).addClass('actived').siblings().removeClass('actived');
        var index = $('.tab-menu li').index(this);
        $('.tab-content .tab-item').eq(index).show().siblings().hide();
    });
    /*-----------------------------------

                    弹窗

    ------------------------------------*/
    // 弹窗
    $('.btn-editor , .btn-modal,.btn-modal2').on('click', function() {
        var ue = UE.getEditor('article-content', {
            initialFrameWidth: 620, //初始化宽度
            initialFrameHeight: 240, //初始化高度
            autoHeightEnabled: false,
        });
        showModal();
    });
    $('.btn-article-back').on('click', function() {
        //恢复回收站信息到正常信息列表
        var $this = $(this);
        var url = $("#updateUrl").val();
        var id = $this.parent().find('input[name=id]').val();
        $.post(url, {
            "id": id,
        }, function(data) {
            $this.closest('tr').remove();
        });
    });
    // 关闭弹窗
    $('#modal').on('click', '.btn-closed , .btn-cancel', function() {
        $('#ueditor_2').html('');
        $('#modal').hide();
        $('#modal .modal-overlay').hide();
        $('#modal .modal-box').hide();
        $mask.hide();

    });
    var tabAction = ["school", "school_departments", "school_info", "school_professional", "school_alumni", "school_address"];
    // 提交
    $('#modal').on('click', '.btn-submit-2', function() {
        $('#modal').hide();
        $('#modal .modal-overlay').hide();
        $('#modal .modal-box').hide();
        $mask.hide();
    });
    $('#modal').on('click', '.btn-submit', function() {
        var $this = $(this);
        var activedTab = $('.tab-menu li.actived');
        var tab_id = $('.tab-menu li').index(activedTab);
        var url = $(this).closest('form').attr('action');
        var jsondata = $(this).closest('form').serializeObject();
        $.extend(jsondata, {
            "action": tabAction[tab_id],
        });
        console.log(jsondata);
        // console.log(url);
        //     
        $.post(url, jsondata, function(data) {
            // console.log(data);
            $('#modal').hide();
            $('#modal .modal-overlay').hide();
            $('#modal .modal-box').hide();
            $mask.hide();
            location.reload();
        });
    });
    // 删除
    $('.tab-content').on('click', '.btn-remove', function(event) {
        var sid = $(this).attr('sid');
        var activedTab = $('.tab-menu li.actived');
        var tab_id = $('.tab-menu li').index(activedTab);
        var url = $("#deleteURL").val();
        var $this = $(this);
        $.post(url, {
            "sid": sid,
            "action": tabAction[tab_id]
        }, function(data) {
            $this.closest('tr').remove();
        });
    });
    //文章删除
    $('.article-content').on('click', '.btn-remove', function(event) {
        var $this = $(this);
        var url = $("#deleteUrl").val();
        var id = $this.parent().find('input[name=id]').val();
        $.post(url, {
            "id": id,
        }, function(data) {
            $this.closest('tr').remove();
        });
    });
    //文章回收站删除
    $('.article-content').on('click', '.btn-delete-remove', function(event) {
        var choice = confirm("您确定要删除吗？(回收站中删除数据无法再找回！点击确定继续删除，取消返回！)", function() {}, null);
        if (choice) {
            var $this = $(this);
            var url = $("#deleteUrl").val();
            var id = $this.parent().find('input[name=id]').val();
            $.post(url, {
                "id": id,
            }, function(data) {
                $this.closest('tr').remove();
            });
        }
    });
    /* --------查看详细评论---------*/
    // 弹窗
    $('.btn-comment').on('click', function() {
        var comment_id = $(this).parent().children("input[name=comment_id]").val();
        $("#comment_id").val(comment_id);
        var url = $("#commentselect").val();
        var $this = $(this);
        $.post(url, {
            "comment_id": comment_id
        }, function(data) {
            $("#comment-title").html(data.user_name + ":" + "<span>" + data.title + "</span>");
            //教学质量
            $("#teaching_int").addClass("star-" + parseInt(data.teaching_int)).attr('data-star', parseInt(data.teaching_int));
            $("#teaching").text(data.teaching);
            $('input[name=teaching]').val(data.teaching);
            //师资力量
            $("#teacher_int").addClass("star-" + parseInt(data.teacher_int)).attr('data-star', parseInt(data.teacher_int));
            $("#teacher").text(data.teacher);
            $('input[name=teacher]').val(data.teacher);
            //设施配套
            $("#facilities_int").addClass("star-" + parseInt(data.facilities_int)).attr('data-star', parseInt(data.facilities_int));
            $("#facilities").text(data.facilities);
            $('input[name=facilities]').val(data.facilities);
            //校园环境
            $("#surrounding_int").addClass("star-" + parseInt(data.surrounding_int)).attr('data-star', parseInt(data.surrounding_int));
            $("#surrounding").text(data.surrounding);
            $('input[name=surrounding]').val(data.surrounding);
            //交通设施
            $("#traffic_int").addClass("star-" + parseInt(data.traffic_int)).attr('data-star', parseInt(data.traffic_int));
            $("#traffic").text(data.traffic);
            $('input[name=traffic]').val(data.traffic);
        });
        showModal();
    });
    //修改弹窗
    $('#modal-comment').on('click', '.btn-editor', function(event) {
        $(this).hide();
        $(this).prev().show();
        var title = $('#comment-title').find("span").html();
        $('#comment-title').find("span").html('<input type="text"  name="" value="' + title + '"/>');
        $(".comment-content").text('');
        $(".comment-content").next('input').show();
        $(".modal-content").find('.star').each(function(index, el) {
            $(el).hide();
            var num = $(this).attr("data-star");
            $(el).next().find('.vote-star').each(function(index2, el2) {
                if (index2 < num) {
                    $(el2).addClass("hover");
                }
            });
        });
        $(".modal-content").find('.star').hide();
        $(".modal-content").find('.vote-box').show();
    });
    //保存弹窗
    $('#modal-comment').on('click', '.btn-save', function(event) {
        $('#modal').hide();
        $('#modal .modal-overlay').hide();
        $('#modal .modal-box').hide();
        $mask.hide();
    });
    // 口碑评分
    $('.vote-box').each(function(index, el) {
        $(el).find('.vote-star').mouseenter(function(e) { //鼠标移入
            $(this).addClass('hover');
            $(this).nextAll().removeClass('hover');
            $(this).prevAll().addClass('hover');
            var num = $(el).find('.vote-star').index(this);
            $(this).closest('li').find('input:hidden').val(num + 1);
            $('#pingjun').val(toAverage());
        }).on('click', function(event) { //点击
            var num = $(el).find('.vote-star').index(this) + 1;
            $(this).parent().prev().attr("data-star", num);
        });
        $(el).mouseleave(function() {
            var num = $(this).prev().attr("data-star");
            $(el).find('.vote-star').each(function(index2, el2) {
                if (index2 < num) {
                    $(el2).addClass("hover");
                } else {
                    $(el2).removeClass("hover");
                }
            });
        });

        function toAverage() {
            var sum = 0;
            var nAverage = 0;
            var $hidden = $('#modal-comment .vote-box').next('input:hidden');
            var len = $hidden.length;
            $hidden.each(function(index2, el2) {
                sum += parseInt($(el2).val());
            });
            nAverage = sum / 5;
            return nAverage;
        }
    });
    //查看详细报名
    // 弹窗
    $('.btn-apply').on('click', function() {
        var apply_id = $(this).parent().children("input[name=apply_id]").val();
        var url = $("#geturl").val();
        var $this = $(this);
        $.post(url, {
            "apply_id": apply_id
        }, function(data) {
            //$("#comment-title").text(data.user_name+":"+data.title);
            //       alert(data);
            $("#name").val(data.name);
            $("#school_name").val(data.school_name);
            $("#birthday").val(data.birthday);
            $("#curator_name").val(data.curator_name);
            $("#curator_phone").val(data.curator_phone);
            $("#plane").val(data.plane);
            $("#qq").val(data.qq);
            $("#email").val(data.email);
            $("#address").html(data.address);
            $("input:radio[name='sex']").eq(data.sex).attr("checked", 'checked');
            $("input:radio[name='curator']").eq(data.curator).attr("checked", 'checked');
        });
        showModal();
    });
    // 删除报名
    $('.tab-content').on('click', '.btn-apply-delete', function(event) {
        var apply_id = $(this).parent().children("input[name=apply_id]").val();
        var url = $("#deleteurl").val();
        var $this = $(this);
        $.post(url, {
            "apply_id": apply_id
        }, function(data) {
            $this.closest('tr').remove();
            //                alert(111)
        }).error(function() {
            alert("error");
        });
    });
    // 删除评论
    $('.tab-content').on('click', '.btn-delete', function(event) {
        var comment_id = $(this).parent().children("input[name=comment_id]").val();
        var url = $("#commentsdelete").val();
        var $this = $(this);
        $.post(url, {
            "comment_id": comment_id
        }, function(data) {
            $this.closest('tr').remove();
            //                        alert(111)
        }).error(function() {
            alert("error");
        });
    });
}); //end
/*-----------------------------------
    
                文章页

------------------------------------*/
function moduleArticle() {
        var $modal = $('#modal');
        // 新增
        $('.btn-modal').on('click', function() {
            $("#update-hide").show();
            $modal.find(".modal-header h2").text("发布文章");
            $modal.find("form").attr('action', $("#addUrl").val());
        });
        // 编辑
        $('.btn-editor').on('click', function() {
            var ue = UE.getEditor('article-content', {
                initialFrameWidth: 620, //初始化宽度
                initialFrameHeight: 240, //初始化高度
                autoHeightEnabled: false,
            });
            var id = $(this).parent().find('input:hidden').val();
            $.post($("#ajaxUrl").val(), {
                'id': id
            }, function(data) {
                $("#modal_school_id").val(data.school_id);
                $("#modal_school_name").val(data.school_name);
                $("#modal_category").val(data.category);
                $("#update-hide").hide();
                $("#article-title").val(data.title);
                $("#article-summary").val(data.description);
                //           $("#article-content").html(data.content);
                ue.ready(function() {
                    ue.setContent(data.content, false); //这里给编辑器添加内容
                });
                //                         $("#article-content").replaceWith("<textarea name='content' id='article-content1' >"+data.content+"</textarea>"); 
                $("#modal_article_id").val(data.id);
            });
            $modal.find(".modal-header h2").text("编辑文章");
            $modal.find("form").attr('action', $("#updateUrl").val());
        });
    }
    /*-----------------------------------
            
                        院系设置

        ------------------------------------*/
function moduleDepartment() {
        // 新增
        var $modal = $('#modal-department');
        $("#tab-item-department").find('.btn-modal').on('click', function() {
            var id = $("#school_id").val();
            $modal.find('form').append('<input type="hidden" name="school_id" value="' + id + '">');
            showModal("modal-department");
            $modal.find(".modal-header h2").text("新增院系");
            $modal.find("#article-title").val("");
            // $modal.find("#article-content").val("");
            var ue = UE.getEditor('article-content', {
                initialFrameWidth: 620, //初始化宽度
                initialFrameHeight: 240, //初始化高度
                autoHeightEnabled: false,
            });
            ue.ready(function() {
                ue.setContent("", false); //这里给编辑器添加内容
            });
            var url = $modal.find("form").attr('addUrl');
            $modal.find("form").attr('action', url);
        });
        // 编辑
        $("#tab-item-department").find('.btn-editor').on('click', function() {
            var title = $(this).closest('tr').find('td').eq(0).text();
            var content = $(this).closest('tr').find('td').eq(1).text();
            var sid = $(this).attr('sid');
            $modal.find('form').append('<input type="hidden" name="departments_id" value="' + sid + '">');
            showModal("modal-department");
            $modal.find(".modal-header h2").text("编辑院系");
            $modal.find("#article-title").val(title);
            var ue = UE.getEditor('article-content', {
                initialFrameWidth: 620, //初始化宽度
                initialFrameHeight: 240, //初始化高度
                autoHeightEnabled: false,
            });
            ue.ready(function() {
                ue.setContent(content, false); //这里给编辑器添加内容
            });
            var url = $modal.find("form").attr('editorUrl');
            $modal.find("form").attr('action', url);
        });
    }
    /*-----------------------------------
            
                        高校详情

        ------------------------------------*/
function moduleDetail() {

        // 编辑
        var $modal = $('#modal-detail');
        $("#tab-item-detail").find('.btn-editor').on('click', function() {
            var title = $(this).closest('tr').find('td').eq(0).text();
            var content = $(this).closest('tr').find('td').eq(1).text();
            var sid = $(this).attr('sid');
            var infoType = $(this).attr('info-type');
            var url;
            
            showModal("modal-detail");
            $modal.find(".modal-header h2").text(title);

            //$modal.find(".modal-content textarea").val(content);
            var ue = UE.getEditor('article-editor', {
                initialFrameWidth: 620, //初始化宽度
                initialFrameHeight: 240, //初始化高度
                autoHeightEnabled: false,
            });
            ue.ready(function() {
                ue.setContent(content, false); //这里给编辑器添加内容
            });

            $modal.find(".modal-content textarea").attr('name', infoType);

            if (sid) {
                // 编辑
                $modal.find('form').append('<input type="hidden" name="info_id" value="' + sid + '">');
                url = $modal.find("form").attr('editorUrl');
            } else{
                // 新增
                url = $modal.find("form").attr('addUrl');
            }

            $modal.find("form").attr('action', url);
        });
    }
    /*-----------------------------------
            
                        优势专业

        ------------------------------------*/
function moduleMajor() {
        // 新增
        var $modal = $('#modal-major');
        $("#tab-item-major").find('.btn-modal').on('click', function() {
            showModal("modal-major");
            var id = $("#school_id").val();
            $modal.find('form').append('<input type="hidden" name="school_id" value="' + id + '">');
            $modal.find(".modal-header h2").text("新增专业");
            $modal.find("#article-title").val("");
            $modal.find("#article-content").val("");
            var ue = UE.getEditor('article-major', {
                initialFrameWidth: 620, //初始化宽度
                initialFrameHeight: 240, //初始化高度
                autoHeightEnabled: false,
            });
            ue.ready(function() {
                ue.setContent("", false); //这里给编辑器添加内容
            });
            var url = $modal.find("form").attr('addUrl');
            $modal.find("form").attr('action', url);
        });
        // 编辑
        $("#tab-item-major").find('.btn-editor').on('click', function() {
            var title = $(this).closest('tr').find('td').eq(0).text();
            var content = $(this).closest('tr').find('td').eq(1).text();
            var sid = $(this).attr('sid');
            $modal.find('form').append('<input type="hidden" name="professional_id" value="' + sid + '">');
            showModal("modal-major");
            $modal.find(".modal-header h2").text("编辑专业");
            $modal.find("#article-title").val(title);
            //            $modal.find("#article-content").val(content);
            var ue = UE.getEditor('article-major', {
                initialFrameWidth: 620, //初始化宽度
                initialFrameHeight: 240, //初始化高度
                autoHeightEnabled: false,
            });
            ue.ready(function() {
                ue.setContent(content, false); //这里给编辑器添加内容
            });
            var url = $modal.find("form").attr('editorUrl');
            $modal.find("form").attr('action', url);
        });
    }
    /*-----------------------------------
            
                        知名校友

        ------------------------------------*/
function moduleAlumni() {
        var ue = UE.getEditor('article-alumni', {
            initialFrameWidth: 620, //初始化宽度
            initialFrameHeight: 240, //初始化高度
            autoHeightEnabled: false,
        });
        // 新增
        var $modal = $('#modal-alumni');
        $("#tab-item-alumni").find('.btn-modal').on('click', function() {
            var id = $("#school_id").val();
            $modal.find('form').append('<input type="hidden" name="school_id" value="' + id + '">');
            showModal("modal-alumni");
            $modal.find(".modal-header h2").text("新增校友");
            $modal.find('#article-title').val("");
            var url = $modal.find("form").attr('addUrl');
        });
        // 编辑
        $("#tab-item-alumni").find('.btn-editor').on('click', function() {
            var title = $(this).closest('tr').find('td').eq(0).find('span').text();
            var imgUrl = $(this).closest('tr').find('td').eq(0).find('img').attr('src');
            var content = $(this).closest('tr').find('td').eq(1).text();
            var sid = $(this).attr('sid');
            $modal.find('form').append('<input type="hidden" name="alumni_id" value="' + sid + '">');
            // console.log(imgUrl);
            showModal("modal-alumni");
            $modal.find(".modal-header h2").text("编辑校友");
            $modal.find('.input-file').find('img').attr('src', imgUrl);
            $modal.find('#article-title').val(title);
            $modal.find('#article-content').val(content);
            ue.ready(function() {
                ue.setContent(content, false); //这里给编辑器添加内容
            });
            var url = $modal.find("form").attr('editorUrl');
            $modal.find("form").attr('action', url);
        });
        // 图片上传预览
        $modal.find('input[type = file]').change(function() {
            var objUrl = getObjectURL(this.files[0]);
            if (objUrl) {
                $("#modal .input-file img").attr("src", objUrl);
            }
        });
    }
    /*-----------------------------------
            
                        学校地址

        ------------------------------------*/
function moduleAddress() {
        // 新增
        var $modal = $('#modal-address');
        $("#tab-item-address").find('.btn-modal').on('click', function() {
            var url = $modal.find("form").attr('addUrl');
            var id = $("#school_id").val();
            $('#province2').val();
            $('#city2').val();
            $('#county2').val();
            $('#address-name').val();
            $('#address-detail').val();
            showModal("modal-address");
            $modal.find(".modal-header h2").text("新增校区");
            $modal.find("form").attr('action', url);
            $modal.find('form').append('<input type="hidden" name="school_id" value="' + id + '">');
            $modal.find('form').append('<input type="hidden" name="mian" value="0">');
            //$modal.find('form').append('<input type="hidden" name="action" value="school_address">');
        });
        // 编辑
        $("#tab-item-address").find('.btn-editor').on('click', function() {
            var url          = $modal.find("form").attr('editorUrl');
            var id           = $("#school_id").val();
            var sid          = $(this).attr('sid');
            var name         = $(this).closest('tr').find('td').eq(0).text();
            var province     = $(this).closest('tr').data('province');
            var city         = $(this).closest('tr').data('city');
            var county       = $(this).closest('tr').data('county');
            var detail       = $(this).closest('tr').data('address');
            var coordinate   = $(this).closest('tr').find('td').eq(2).text();
            var zip          = $(this).closest('tr').data('zip');
            var coordinate_x = $(this).closest('tr').data('coordinatex');
            var coordinate_y = $(this).closest('tr').data('coordinatey');
            var phone = $(this).closest('tr').data('phone');
            $('#province2').val(province);
            change2(1);
            $('#city2').val(city);
            change2(2);
            $('#county2').val(county);
            $('#address-name').val(name);
            $('#address-detail').val(detail);
            $('#coordinate_x').val(coordinate_x);
            $('#coordinate_y').val(coordinate_y);
            $('#zip').val(zip);
            $('#phone').val(phone);
            $modal.find(".modal-header h2").text("编辑校区");
            $modal.find("form").attr('action', url);
            if ($modal.find('form').find('input[name=address_id]')[0]) {
                $modal.find('form').find('input[name=address_id]').val(sid);
            } else {
                $modal.find('form').append('<input type="hidden" name="address_id" value="' + sid + '">');
            }
            $modal.find('form').append('<input type="hidden" name="school_id" value="' + id + '">');
            showModal("modal-address");
        });
        // 设为主校区
        $("#tab-item-address").find('.btn-mainschool').on('click', function() {
            var url = $("#editorURL").val();
            var sid = $(this).attr("sid");
            var school_id = $("#school_id").val();
            var $this = $(this);
            $.post(url, {
                "action": "school_address",
                "school_id": school_id,
                "address_id": sid,
                "main": 1
            }, function(data) {
                console.log(data);
                location.reload();
            });
        });
        // 地址提交
        $('#modal').on('click', '.btn-submit2', function() {});
    }
    /*-----------------------------------
        
                    新增学校

    ------------------------------------*/
function moduleAddLogo() {
        // 图片上传预览
        $('#upload-school-logo').find('input[type = file]').change(function() {
            var objUrl = getObjectURL(this.files[0]);
            if (objUrl) {
                $("#school-logo").attr("src", objUrl);
            }
        });
    }
    /*-----------------------相关函数----------------------------------*/
    //获取图片地址函数
function getObjectURL(file) {
        var url = null;
        if (window.createObjectURL !== undefined) { // basic
            url = window.createObjectURL(file);
        } else if (window.URL !== undefined) { // mozilla(firefox)
            url = window.URL.createObjectURL(file);
        } else if (window.webkitURL !== undefined) { // webkit or chrome
            url = window.webkitURL.createObjectURL(file);
        }
        return url;
    }
    // 弹框函数
function showModal(modalBox) {
        $('#modal').show();
        $('#modal .modal-overlay').show();
        if ($('#modal .modal-box').length === 1) {
            $('#modal .modal-box').show();
        } else if ($('#modal .modal-box').length > 1) {
            if ($("#" + modalBox).length > 0) {
                $("#" + modalBox).show();
            }
        }
        $mask = $(window.top.document).find('#mask'); //全局变量
        $mask.show();
        turnCenter();
        return false;
    }
    // 修复点击导航栏,侧边栏改版，主内容不动
$(function() {
    var link = $('.submenu li.actived').find('a').attr('href');
    $('#main', parent.document.body).attr('src', link);
});
// 模态框居中函数
$(window).resize(turnCenter);

function turnCenter() {
        var leftFrameWidth = 190;
        var topFrameHeight = 160;
        $('.modal-box').each(function(index, el) {
            $(el).css({
                position: 'fixed',
                left: ($(window).width() - $(el).outerWidth() - leftFrameWidth) / 2,
                top: ($(window).height() - $(el).outerHeight() - topFrameHeight) / 2
            });
        });
    }
    // 表单序列化
$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};
